package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.service.DtsCouponService;
import com.qf.service.DtsCouponUserService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zwnan
 */
@RestController
@RequestMapping("/admin/coupon")
@CrossOrigin
public class CouponController {

    @Autowired
    private DtsCouponService dtsCouponService;
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button ="查找")
    @RequiresPermissions("admin:coupon:list")
    @GetMapping("/list")
    public Object List(@RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order){
        IPage<DtsCoupon> iPage = dtsCouponService.findPage(page,limit,sort,order);
        long total = iPage.getTotal();
        List<DtsCoupon> records = iPage.getRecords();
        Map map = new HashMap();
        map.put("items", records);
        map.put("total", total);
        return ResponseUtil.ok(map);

    }
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button ="详情")
    @RequiresPermissions("admin:coupon:read")
    @GetMapping("read")
    public Object read(String id){
        DtsCoupon dtsCoupon = dtsCouponService.findById(id);
        return ResponseUtil.ok(dtsCoupon);
    }

    @Autowired
    private DtsCouponUserService dtsCouponUserService;

    @GetMapping("listuser")
    public Object listuser(@RequestParam(defaultValue = "1")Integer page,
                           @RequestParam(defaultValue = "20") Integer limit,
                           @RequestParam(defaultValue = "add_time") String sort,
                           @RequestParam(defaultValue = "desc") String order,
                           String couponId){
            IPage<DtsCouponUser> iPage = dtsCouponUserService.findPage(page,limit,sort,order,couponId);
        List<DtsCouponUser> records = iPage.getRecords();
        long total = iPage.getTotal();
        Map map = new HashMap();
        map.put("total", total);
        map.put("items", records);
        return ResponseUtil.ok(map);
    }
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button ="添加")
    @RequiresPermissions("admin:coupon:create")
    @PostMapping("/create")
    public Object create(@RequestBody DtsCoupon dtsCoupon){
        dtsCouponService.insert(dtsCoupon);
        return ResponseUtil.ok();
    }
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button ="编辑")
    @RequiresPermissions("admin:coupon:update")
    @PostMapping("/update")
    public Object update(@RequestBody DtsCoupon dtsCoupon){
        dtsCouponService.update(dtsCoupon);
        return ResponseUtil.ok();
    }
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button ="删除")
    @RequiresPermissions("admin:coupon:delete")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCoupon dtsCoupon){
        dtsCouponService.delete(dtsCoupon);
        return ResponseUtil.ok();
    }
}
